package br.com.cicero.notasfiscais.mb;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.cicero.notasfiscais.annotation.EmailFinanceiro;
import br.com.cicero.notasfiscais.dao.DAO;
import br.com.cicero.notasfiscais.modelo.Item;
import br.com.cicero.notasfiscais.modelo.NotaFiscal;

@Named @ConversationScoped
public class NotaFiscalBeanConversation implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	private Conversation conversation;
	private NotaFiscal notaFiscal = new NotaFiscal();
	private Item item = new Item();
	private HtmlDataTable tabela;
	
	@Inject @EmailFinanceiro
	private String email;
	
	@Inject
	private DAO<NotaFiscal> dao;
		
	public String gravar() {
		dao.adiciona(notaFiscal);
		conversation.end();
		System.out.println("Notifica " + email + ": nova NotaFiscal cadastrada.");
		return "notafiscal-conversation?faces-redirect=true";
	}
	
	public void guardaItem() {
		item.setValorUnitario(item.getProduto().getPreco());
		
		notaFiscal.getItens().add(item);
		item.setNotaFiscal(notaFiscal);
		
		item = new Item();
	}
	
	public void removeItem() {
		Item item = (Item) tabela.getRowData();
		notaFiscal.getItens().remove(item);
	}
	
	public String avancar() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
		return "item-nota-fiscal-conversation?faces-redirect=true";
	}
	
	public NotaFiscal getNotaFiscal() {
		return notaFiscal;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}
	
	public void setNotaFiscal(NotaFiscal notaFiscal) {
		this.notaFiscal = notaFiscal;
	}

	public HtmlDataTable getTabela() {
		return tabela;
	}

	public void setTabela(HtmlDataTable tabela) {
		this.tabela = tabela;
	}
		
}
